package at.fhj.pse.datasource.model;

import javax.persistence.Entity;

import java.io.Serializable;

import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Column;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import at.fhj.pse.datasource.exception.DatasourceLayerException;

import java.lang.Override;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

@Entity
public class TimeInterval implements Serializable, Comparable<TimeInterval> {

	private static final long serialVersionUID = -8567559351155655078L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id", updatable = false, nullable = false)
	private Long id = null;

	@Column(nullable = false)
	private Date to_Date;
	
	@Column(nullable = false)
	private Date from_Date;
	
	@Column
	private String comment;
	

	@ManyToOne(fetch=FetchType.LAZY)
	private Employee employee;
	
	@OneToOne(fetch=FetchType.LAZY)
	private Activity activity;

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		if (activity == null) {
			throw new DatasourceLayerException("activity is null", new IllegalArgumentException());
		}
		this.activity = activity;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		if (employee == null) {
			throw new DatasourceLayerException("employee is null", new IllegalArgumentException());
		}
		this.employee = employee;
	}

	public Long getId() {
		return this.id;
	}

	public void setId(final Long id) {
		this.id = id;
	}

	public Date getTo() {
		return to_Date;
	}

	public void setTo(Date to) {
		if (to == null) {
			throw new DatasourceLayerException("to is null", new IllegalArgumentException());
		}
		this.to_Date = to;
	}
	
	public String getIntervalLenghtAsString()
	{
		if(!this.activity.getDescription().equals("Zeitausgleich")){
	Calendar cal = Calendar.getInstance();
	long lengthInMillis = to_Date.getTime() - from_Date.getTime();
	cal.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
	cal.setTimeInMillis(lengthInMillis);
	
	return cal.get(Calendar.HOUR_OF_DAY) + "h " + cal.get(Calendar.MINUTE) + "min";
		}
		return "0h 0min";
	}

	public Date getFrom() {
		return from_Date;
	}

	public void setFrom(Date from) {
		if (from == null) {
			throw new DatasourceLayerException("from is null", new IllegalArgumentException());
		}
		this.from_Date = from;
	}

	public String getComment() {
		return comment;
	}
	
	public void setComment(String comment) {
		if (comment == null) {
			throw new DatasourceLayerException("comment is null", new IllegalArgumentException());
		}
		this.comment = comment;
	}
	
	@Override
	public boolean equals(Object that) {
		if (this == that) {
			return true;
		}
		if (that == null) {
			return false;
		}
		if (getClass() != that.getClass()) {
			return false;
		}
		if (id != null) {
			return id.equals(((TimeInterval) that).id);
		}
		return super.equals(that);
	}

	@Override
	public int hashCode() {
		if (id != null) {
			return id.hashCode();
		}
		return super.hashCode();
	}

	@Override
	public String toString() {
		return this.from_Date.toString() + " - " + this.to_Date.toString();
	}

	@Override
	public int compareTo(TimeInterval o) {
		if (o == null) {
			throw new DatasourceLayerException("(o) TimeInterval is null", new IllegalArgumentException());
		}
		return (int) (this.getFrom().getTime() - o.getFrom().getTime());
	}
}